package com.hankol.community.adapter

import android.app.Activity
import android.widget.TextView
import com.baselibrary.BaseConstant
import com.baselibrary.bean.FollowSuccBean
import com.baselibrary.net.APIResponse
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.ARouterUtils
import com.baselibrary.utils.DoubleClickHelper
import com.baselibrary.view.VipHeardImgView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.hankol.community.R

import com.hankol.community.bean.InterestUserBean
import com.baselibrary.utils.SpanUtils
import com.hankol.message.HKIMUtils
import com.hankol.community.viewmodel.CommunityViewModel
import java.util.HashMap


/**
 *@author:hanke
 *2022/3/30
 *desc:
 */
class RecommendUserAdapter(val activity: Activity, val communityViewModel: CommunityViewModel) : BaseQuickAdapter<InterestUserBean, BaseViewHolder>(R.layout.layout_interest_users) {

    override fun convert(helper: BaseViewHolder, item: InterestUserBean) {
        helper.setText(R.id.tv_recommend_name, SpanUtils.appendEllipsis(item.recommendUser.name, 5))
        helper.getView<VipHeardImgView>(R.id.circle_attentive_head).setData(item.recommendUser.icon, item.recommendUser.isFans == 1)
        helper.setOnClickListener(R.id.circle_attentive_head) {
            // 跳转 用户详情
            val hashMap = HashMap<String, Any>()
            hashMap[BaseConstant.USER_ID] = item.recommendUser.id.toString()
            ARouterUtils.jump(ActivityRouter.MINE_CENTER.USER_INFO, hashMap)
        }
        helper.setText(R.id.tv_recommend_tip, "${SpanUtils.appendEllipsis(item.sourceUser.name, 4)}关注了${SpanUtils.appendEllipsis(item.recommendUser.name, 4)}")
        val focusBtn: TextView = helper.getView(R.id.tv_focus)
        focusBtn.setOnClickListener {
            if (!DoubleClickHelper.isOnDoubleClick()) {
                val action: Boolean = !item.recommendUser.hasFocus
                communityViewModel.followAction2(activity, action, item.recommendUser.id, object : APIResponse<FollowSuccBean>(this) {
                    override fun onSuccess(result: FollowSuccBean?, json: String?) {
                        item.recommendUser.hasFocus = action
                        if (!item.recommendUser.hasFocus) {
                            focusBtn.text = "关注"
                            focusBtn.isSelected = false
                        } else {
                            focusBtn.text = "已关注"
                            focusBtn.isSelected = true
                            //关注成功--发送消息
                            result?.let {
                                HKIMUtils.followSendMsg(item.recommendUser.id.toInt(), result.friendState)
                            }
                        }
                    }
                })
            }
        }
    }
}